class Npc {

    view = new renderer.DisplayObjectContainer();

    x = 0;

    y = 0;

    id = 0;

    name = "";

    canAcceptMissions: Mission[] = [];

    canSubmitMissions: Mission[] = [];

    constructor(config: any) {
        this.id = config.id;
        this.name = config.name;
        const avatarUrl = config.avatar;

        const texture = new Image();
        texture.src = avatarUrl;

        const bitmap = new renderer.Bitmap(0, 0, texture);
        this.view.addChild(bitmap);
        const textField = new renderer.TextField();
        textField.text = this.name;
        this.view.addChild(textField);
        textField.x = 20;
        textField.y = 40;

        missionManager.addEventListener("missionUpdate", () => {
            this.update();
        })
        this.update();

    }

    private update() {
        this.canAcceptMissions = [];
        this.canSubmitMissions = [];
        for (let mission of missionManager.missions) {
            if (mission.status == MissionStatus.CAN_ACCEPT && mission.fromNpcId == this.id) {
                this.canAcceptMissions.push(mission);

            }
            if (mission.status == MissionStatus.CAN_SUBMIT && mission.toNpcId == this.id) {
                this.canSubmitMissions.push(mission);
            }
        }
    }


    toString() {
        return `[NPC: id:${this.id}]`
    }
}

class NpcManager {



    public npcs: Npc[] = [];

    parseFromConfig(obj: any) {

        for (let item of obj.npc) {
            const npc = new Npc(item)
            this.npcs.push(npc)
        }
    }

    getNpcById(id: number) {
        for (let npc of this.npcs) {
            if (npc.id == id) {
                return npc;
            }
        }
        throw `missing npcid : ${id}`;
    }

}


const npcManager = new NpcManager();